<template>
  <div class="admin-layout">
    <!-- 侧边栏 -->
    <el-aside width="200px" class="sidebar">
      <div class="logo">管理系统</div>
      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        background-color="#ffffff"
        text-color="#333333"
        active-text-color="#409eff"
        router
        :unique-opened="true">
        <template v-for="item in menuItems" :key="item.index">
          <el-menu-item 
            v-if="!item.children || item.children.length === 0"
            :index="item.index"
          >
            <el-icon>
              <component :is="item.icon" />
            </el-icon>
            <span>{{ item.title }}</span>
          </el-menu-item>
          <el-sub-menu 
            v-else
            :index="item.index"
            popper-append-to-body
            popper-class="sidebar-sub-menu"
          >
            <template #title>
              <el-icon>
                <component :is="item.icon" />
              </el-icon>
              <span>{{ item.title }}</span>
            </template>
            <el-menu-item 
              v-for="child in item.children" 
              :key="child.index" 
              :index="child.index"
            >
              <el-icon>
                <component :is="child.icon" />
              </el-icon>
              <span>{{ child.title }}</span>
            </el-menu-item>
          </el-sub-menu>
        </template>
      </el-menu>
    </el-aside>
    
    <!-- 右侧内容区 -->
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header class="header">
        <div class="header-content">
          <div class="header-title">管理后台</div>
          <div class="header-actions">
            <el-color-picker 
              v-model="themeColor" 
              @change="changeTheme" 
              :predefine="predefineColors"
            />
            <el-dropdown>
              <span class="el-dropdown-link">
                管理员
                <el-icon class="el-icon--right">
                  <arrow-down />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      
      <!-- 主要内容区 -->
      <el-main class="main-content">
        <router-view />
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { House, User, Setting, ArrowDown } from '@element-plus/icons-vue'
import { useThemeStore } from '../stores/theme'
// 从静态JSON文件导入菜单数据
import menuItems from '../assets/menu.json'

const router = useRouter()
const activeMenu = ref('/home')
const themeStore = useThemeStore()
const themeColor = ref(themeStore.themeColor)

// 预定义颜色
const predefineColors = [
  '#409eff',
  '#1890ff',
  '#304156',
  '#212121',
  '#11a983',
  '#13c2c2',
  '#6959CD',
  '#f5222d',
]

const handleLogout = () => {
  // 清除登录状态
  localStorage.removeItem('isAuthenticated')
  // 跳转到登录页
  router.push('/login')
}

const changeTheme = (color: string | null) => {
  if (color) {
    themeStore.setThemeColor(color)
  }
}

// 组件挂载时同步主题颜色
onMounted(() => {
  themeColor.value = themeStore.themeColor
})
</script>

<style scoped>
.admin-layout {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.sidebar {
  background-color: #ffffff;
  color: #333333;
  overflow: hidden;
  min-height: 100vh;
  border-right: 1px solid #e6e6e6;
}

.logo {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  border-bottom: 1px solid #434b54;
}

.sidebar-menu {
  border: none;
  height: calc(100% - 60px);
  overflow: visible;
}

.sidebar-sub-menu {
  background-color: #ffffff;
  color: #333333;
}

.header {
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 0;
  min-width: 1000px; /* 确保顶部导航栏有足够的宽度 */
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.header-title {
  font-size: 18px;
  font-weight: bold;
}

/* 主题颜色选择器样式 */
.header-actions {
  display: flex;
  align-items: center;
  gap: 20px;
}

/* 调整颜色选择器大小 */
.el-color-picker {
  height: 32px;
}

.el-color-picker__trigger {
  width: 32px;
  height: 32px;
  border-radius: 4px;
}

.main-content {
  background-color: #f5f5f5;
  padding: 20px;
  overflow: auto;
  flex: 1;
  min-width: 1000px; /* 确保内容区域有足够的宽度 */
  min-height: calc(100vh - 60px); /* 减去顶部导航栏的高度 */
}
</style>